﻿#include "OrderSystem.h"

OrderSystem::OrderSystem()
{
	this->m_ComputerRoomFile = new ComputerRoomFile();
	this->m_OrderFile = new OrderFile();
}
OrderSystem::~OrderSystem()
{
	if (this->m_ComputerRoomFile != NULL)
	{
		delete this->m_ComputerRoomFile;
		this->m_ComputerRoomFile = NULL;
	}
	if (this->m_OrderFile != NULL)
	{
		delete this->m_OrderFile;
		this->m_OrderFile = NULL;
	}
}
// 展示菜单
void OrderSystem::showMenu()
{
	cout << "======================  欢迎来到ADAMGUO机房预约系统  ====================="
		<< endl;
	cout << endl << "请输入您的身份" << endl;
	cout << "\t\t -------------------------------\n";
	cout << "\t\t|                               |\n";
	cout << "\t\t|          1.学    生           |\n";
	cout << "\t\t|                               |\n";
	cout << "\t\t|          2.老    师           |\n";
	cout << "\t\t|                               |\n";
	cout << "\t\t|          3.管 理 员           |\n";
	cout << "\t\t|                               |\n";
	cout << "\t\t|          0.退    出           |\n";
	cout << "\t\t|                               |\n";
	cout << "\t\t -------------------------------\n";
	cout << "输入您的选择: ";
}

// 退出系统
void OrderSystem::exitSystem()
{
	cout << "欢迎下次使用！" << endl;
	system("pause");
	exit(0);
}

// 登录
// 参数1：操作的文件名 参数2：身份（1-学生 2-老师 3-管理员）
void OrderSystem::loginIn(string fileName, int identity)
{
	Identity* person = NULL;

	ifstream ifs;
	ifs.open(fileName, ios::in);

	if (!ifs.is_open())
	{
		cout << "文件不存在！" << endl;
		ifs.close();
		return;
	}

	int id;
	string name;
	string pwd;

	if (identity == 1)
	{
		cout << "请输入学生学号：" << endl;
		cin >> id;
	}
	else if (identity == 2)
	{
		cout << "请输入老师编号：" << endl;
		cin >> id;
	}

	cout << "请输入用户名：" << endl;
	cin >> name;
	cout << "请输入密码：" << endl;
	cin >> pwd;

	if (identity == 1)
	{
		// 学生验证登录
		int fId;
		string fName;
		string fPwd;
		while (ifs >> fId && ifs >> fName && ifs >> fPwd)
		{
			//cout << fId << fName << fPwd << endl;
			if (id == fId && name == fName && pwd == fPwd)
			{
				cout << "学生验证登录成功！" << endl;
				system("pause");
				system("cls");
				person = new Student(id, name, pwd, this->m_OrderFile, this->m_ComputerRoomFile);
				// 转到学生菜单界面
				this->studentMenu(person);
				return;
			}
		}
	}
	else if (identity == 2)
	{
		// 老师验证登录
		int fId;
		string fName;
		string fPwd;
		while (ifs >> fId && ifs >> fName && ifs >> fPwd)
		{
			//cout << fId << fName << fPwd << endl;
			if (id == fId && name == fName && pwd == fPwd)
			{
				cout << "老师验证登录成功！" << endl;
				system("pause");
				system("cls");
				person = new Teacher(id, name, pwd, this->m_OrderFile, this->m_ComputerRoomFile);
				// 转到老师菜单界面
				this->teacherMenu(person);
				return;
			}
		}
	}
	else if (identity == 3)
	{
		// 管理员验证登录
		string fName;
		string fPwd;
		while (ifs >> fName && ifs >> fPwd)
		{
			//cout << fName << fPwd << endl;
			if (name == fName && pwd == fPwd)
			{
				cout << "管理员验证登录成功！" << endl;
				system("pause");
				system("cls");
				person = new Manager(name, pwd, this->m_ComputerRoomFile);
				// 转到管理员菜单界面
				this->managerMenu(person);
				return;
			}
		}
	}

	cout << "验证登陆失败！" << endl;
	system("pause");
	system("cls");
}

// 管理员菜单
void OrderSystem::managerMenu(Identity*& person)
{
	while (true)
	{
		person->operMenu();// 展示自己的菜单
		Manager* manager = (Manager*)person;// 强转成子类，以调用自己的函数
		int select;
		cin >> select;
		if (select == 1)// 添加账号
		{
			manager->addPerson();
		}
		else if (select == 2)// 展示账号信息
		{
			manager->showPerson();
		}
		else if (select == 3)// 展示机房信息
		{
			manager->showComputerRoom();
		}
		else if (select == 4)// 清空预约信息
		{
			manager->cleanFile();
		}
		else// 注销用户
		{
			delete manager;
			manager = NULL;
			cout << "注销成功！" << endl;
			system("pause");
			system("cls");
			break;
		}
	}
}

// 学生菜单
void OrderSystem::studentMenu(Identity*& person)
{
	while (true)
	{
		person->operMenu();
		Student* student = (Student*)person;
		int select;
		cin >> select;
		if (select == 1)// 申请预约
		{
			student->applyOrder();
		}
		else if (select == 2)// 展示我的预约
		{
			student->showMyOrder();
		}
		else if (select == 3)// 展示所有预约
		{
			student->showAllOrder();
		}
		else if (select == 4)// 取消预约
		{
			student->cancelOrder();
		}
		else// 注销用户
		{
			delete student;
			student = NULL;
			cout << "注销成功！" << endl;
			system("pause");
			system("cls");
			break;
		}
	}
}

// 老师菜单
void OrderSystem::teacherMenu(Identity*& person)
{
	while (true)
	{
		person->operMenu();
		Teacher* teacher = (Teacher*)person;
		int select;
		cin >> select;
		if (select == 1)// 查看所有预约
		{
			teacher->showAllOrder();
		}
		else if (select == 2)// 审核预约
		{
			teacher->reviewOrder();
		}
		else// 注销用户
		{
			delete teacher;
			teacher = NULL;
			cout << "注销成功！" << endl;
			system("pause");
			system("cls");
			break;
		}
	}
}